<template>
	<view class="mainpadding beijingse">
		<view class="mainpadding radius ffffff sahngbj" v-for="item in addressList" :key="item.id"
			@click="checkaddress(item.id)">
			<view class="flexleft">
				<view class="titletext fonweight xiaohei margin_right4">{{item.name}}</view>
				<view class="titletext fonweight xiaohei">{{item.mobile}}</view>
			</view>
			<view class="flexleft margin_top xiahuaxian margin_top">
				<view class="margin_right1">
					<u-icon name="map" color="#FF9341" size="20"></u-icon>
				</view>
				<view class="ershil xiaohei">{{item.cityinfo}}/{{item.address_detail}}</view>
			</view>
			<view class="flexbetween margin_top">
				<view class="flexleft" @click.stop="selectmr(item)">
					<!-- 未选中 -->
					<view class="weixz margin_right1" v-if="item.status!=1"></view>
					<!-- 已选中 -->
					<view class="margin_right1" v-if="item.status==1">
						<u-icon name="checkmark-circle-fill" color="#FF8000" size="16"></u-icon>
					</view>
					<view class="strongtext xiaohei">设为默认</view>
				</view>
				<view class="flexright">
					<view class="flexleft margin_right3" @click.stop="editAddress(item.id)">
						<view class="margin_right1">
							<u-icon name="file-text" color="#666666" size="19"></u-icon>
						</view>
						<view class="strongtext xiaohei">编辑</view>
					</view>
					<view class="flexleft" @click.stop="shanchu(item.id)">
						<view class="margin_right1">
							<u-icon name="trash" color="#666666" size="19"></u-icon>
						</view>
						<view class="strongtext xiaohei">删除</view>
					</view>
				</view>
			</view>
		</view>
		<!--  -->
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="tzxzshdz">+新增收货地址</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				type: 0,
				addressList: [],
			}
		},
		onLoad(options) {
			if (options.type) {
				this.type = options.type
			}
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			checkaddress(id){
				if(this.type==1){
					uni.$emit("checkAddressId",id)
					uni.navigateBack(1)
				}
			},
			// 设置默认地址
			selectmr(item) {
				if (item.status == 1) {
					return false
				}
				uni.showLoading({
					title: '请稍候...',
					mask: true
				})
				httpRequest.request('/api/user/setAddressMr', 'GET', {
					id: item.id,
					status: item.status == 0 ? 1 : 0,
				}, false, false, true).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						httpRequest.toast(res.msg)
						this.getAddressList()
					} else {
						httpRequest.toast(res.msg)
					}
				})
			},
			editAddress(id) { //编辑收货地址
				uni.navigateTo({
					url: "/pages_homepage/xinzenshdz?id=" + id
				})
			},
			// 删除地址
			shanchu(id) {
				httpRequest.modal('提示', '确认删除当前数据吗？', true, (res) => {
					if (res) {
						httpRequest.request('/api/user/deladdress', 'GET', {
							id: id
						}, false, false, true).then(res => {
							if (res.code == 1) {
								httpRequest.toast(res.msg)
								this.getAddressList()
							} else {
								httpRequest.toast(res.msg)
							}
						})
					}
				})
			},
			getAddressList() { //地址列表
				httpRequest.request('/api/user/addressList', 'GET', {}).then(res => {
					if (res.code == 1) {
						console.log(res.data)
						this.addressList = res.data
					}
				})
			},
			// 跳转新增收货地址
			tzxzshdz() {
				uni.navigateTo({
					url: '/pages_homepage/xinzenshdz'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.sahngbj {
		margin-top: 30rpx;
	}

	.sahngbj:first-child {
		margin-top: 0rpx;
	}
</style>